﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net.Nntp
{
    using System.Globalization;
    using System.Text;

    /// <summary>Provides helper methods to create fake NNTP servers.</summary>
    internal static class NntpFakeServerHelper
    {
        private static readonly byte[] availablePostingAllowedGreeting =
            Encoding.UTF8.GetBytes("200 Posting allowed\r\n");

        private static readonly byte[] availablePostingProhibitedGreeting =
            Encoding.UTF8.GetBytes("201 Posting prohibited\r\n");

        private const string UnavailableGreeting = "{0} Service unavailable\r\n";
        private static readonly byte[] modeReaderCommand = Encoding.UTF8.GetBytes("MODE READER\r\n");

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal static FakeServer MakeAvailableServer(bool postingAllowed)
        {
            byte[] greeting = postingAllowed ? availablePostingAllowedGreeting : availablePostingProhibitedGreeting;
            FakeServer result =
                new FakeServer(119, Encoding.UTF8, stream => stream.Write(greeting, 0, greeting.Length));
            result.QueueCommand(modeReaderCommand, greeting);
            return result;
        }

        internal static FakeServer MakeUnavailableServer(int responseCode)
        {
            byte[] greeting = Encoding.UTF8.GetBytes(
                string.Format(CultureInfo.InvariantCulture, UnavailableGreeting, responseCode));
            FakeServer result = new FakeServer(
                119,
                Encoding.UTF8,
                stream =>
                {
                    stream.Write(greeting, 0, greeting.Length);
                    stream.Dispose();
                });
            return result;
        }
    }
}
